﻿Namespace PL.MetaSDK 'Processing Layer
    Public Class MetaControls_PL
        Inherits PLBase
        Implements IPLProcess


        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Overrides Function Delete() As Boolean
            Return True
        End Function

        Public Overrides Function Validate() As Boolean
            Return True
        End Function

        Public Shared Function GetNextSequence(ByVal groupID As Guid, ByVal classID As Guid, ByVal categoryID As Guid, ByVal typeID As Guid) As String
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Dim reader As SqlDataReader = Nothing
            Dim metaControlCD As String = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("MetaControls_GetNextSequence")

                If sqlcmd IsNot Nothing Then

                    sqlcmd.Parameters("@GroupId_FK").Value = groupID
                    sqlcmd.Parameters("@ClassId_FK").Value = classID
                    sqlcmd.Parameters("@CategorieId_FK").Value = categoryID
                    sqlcmd.Parameters("@ControlTypeID_FK").Value = typeID

                    reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
                    If reader.Read() Then
                        metaControlCD = reader("ControlCD").ToString
                    End If

                    Return metaControlCD
                End If
            Catch ex As Exception
                Throw
            Finally
                reader.Close()
                reader = Nothing
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                    sqlcmd = Nothing
                End If
            End Try

            Return metaControlCD

        End Function

        Public Shared Function ExportMetaControls(ByVal filename As String, ByVal controlID As Guid) As Boolean
            Using ms As MetaControls_PL = New MetaControls_PL("MetaControlsExport")
                Try
                    ms.MetaDataTable.GetMetaCommand()
                    ms.MetaDataTable.MetaCommand.SQLWhere = String.Format("ControlID = '{0}'", controlID)
                    ms.InitDataForExport()
                    ms.Dataset.DataSetName = controlID.ToString
                    ms.Dataset.WriteXml(filename, XmlWriteMode.WriteSchema)
                    Return True
                Catch ex As Exception
                    Throw
                End Try
                Return False
            End Using
        End Function

        Public Shared Function ExportMetaControlsToMaster(ByVal controlID As Guid) As Boolean
            Dim IsSaved As Boolean
            Using mcSource As MetaControls_PL = New MetaControls_PL("MetaControlsExport")
                Try
                    mcSource.MetaDataTable.GetMetaCommand()
                    mcSource.MetaDataTable.MetaCommand.SQLWhere = String.Format("ControlID = '{0}'", controlID)
                    mcSource.InitDataForExport()
                    mcSource.Dataset.DataSetName = controlID.ToString
                    For Each oTable As DataTable In mcSource.Dataset.Tables
                        For Each oRow As DataRow In oTable.Rows
                            oRow.SetAdded()
                        Next
                    Next


                    'Bypass pour ne plus utiliser la connection par défaut
                    AppManager.IsConnected = False
                    AppManager.OverrideByPassConnectionStringTOMaster = True

                    'Indique qu'elle connection utiliser dans le App.Config
                    AppManager.ConnectionConfigName = "ConnectionToMyMaster"
                    Using mcDest As MetaControls_PL = New MetaControls_PL("MetaControlsExport")
                        mcDest.InitData()
                        mcDest.Dataset.Merge(mcSource.Dataset)
                        IsSaved = mcDest.Save()
                    End Using
                    Return IsSaved
                Catch ex As Exception
                    Throw ex
                Finally

                    'Remet en route la connection par défaut
                    AppManager.IsConnected = True
                    AppManager.OverrideByPassConnectionStringTOMaster = False

                    'Change pour utiliser la connection par défaut
                    AppManager.ConnectionConfigName = Nothing
                End Try
                Return False
            End Using
        End Function

        Public Shared Function ImportMetaControls(ByVal fileName As String) As Boolean
            Using dset As DataSet = New DataSet()
                dset.ReadXml(fileName, XmlReadMode.ReadSchema)
                Using ms As MetaControls_PL = New MetaControls_PL("MetaControlsExport")
                    'Hack: DataSet must be fill to have adaptercommand and merge...
                    ms.InitData()
                    ms.Dataset.Merge(dset)
                    Return ms.Save()
                End Using
            End Using
        End Function

        Public Function InitDataForExport() As Boolean
            Dim metaDataTableChild As CommonExt.MetaDataTable
            Dim aChildRelationField As String
            Dim parentGuid As Guid = Guid.Empty
            Try
                Me.MetaDataTable.FillBusinessApplicationData()
                Me.Dataset.Tables.Add(Me.MetaDataTable)
                'ici on veux un record seulement
                If Not String.IsNullOrEmpty(Me.MetaDataTable.MetaCommand.SQLWhere) Then
                    parentGuid = CType(Me.MetaDataTable.Rows(0).Item(0), Guid)
                End If
                Dim aParentRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField
                For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
                    metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
                    aChildRelationField = Me.MetaDataTable.MetaCommand.GUIDField & "_FK"
                    '
                    metaDataTableChild.GetMetaCommand()
                    '
                    If parentGuid <> Guid.Empty Then
                        metaDataTableChild.MetaCommand.SQLWhere = String.Format("{0} = '{1}'", aChildRelationField, parentGuid)
                    End If
                    '
                    metaDataTableChild.FillBusinessApplicationData()
                    '
                    Me.Dataset.Tables.Add(metaDataTableChild)
                    Dim aDR As New DataRelation(row("Relation").ToString, Me.MetaDataTable.Columns(aParentRelationField), metaDataTableChild.Columns(aChildRelationField))
                    Me.Dataset.Relations.Add(aDR)
                Next
                Return True
            Catch ex As Exception
                Throw
            End Try
            Return False
        End Function


        Public Function BatchRecords(dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords

        End Function

        Public Function CurrentRecord(dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord

        End Function

        Public Function NewRecord(dataRow As DataRow) As Boolean Implements IPLProcess.NewRecord

            dataRow(MyBase.GuidFieldSTR) = Guid.NewGuid

            Return True

        End Function
    End Class
End Namespace
